<template>
  <el-dialog v-model="visible" :title="title" width="720" show-footer show-zoom resize :close-on-click-modal="false"
    :close-on-press-escape="false" @close="close">
    <template #default>
      <el-form ref="formRef" :rules="rules" :model="form" label-align="right" label-position="top" label-width="60"
        size="default">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" :disabled="handleType == 'view'"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <el-input type="textarea" v-model="form.content" placeholder="请输入内容" :autosize="{ minRows: 6, maxRows: 6 }"
            :disabled="handleType == 'view'"></el-input>
        </el-form-item>
        <!-- <el-form-item label="文件"> -->
          <div class="font-bold w-full">
              <FileUpload v-model="form.file" :isShowTip="false" :disabled="handleType == 'view'"></FileUpload>
            <!-- <div class="border" v-if="form.file.length == 0">
              <el-empty description="暂无文件" />
            </div> -->
          </div>
        <!-- </el-form-item> -->
      </el-form>

    </template>

    <template #footer>
      <template v-if="handleType == 'view'">
        <div class="flex justify-center py-2">
          <el-button type="" @click="close">关 闭</el-button>
        </div>
      </template>
      <div class="flex justify-center py-2" v-else>
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" :loading="loading" @click="save(formRef)">保 存</el-button>
      </div>

    </template>
  </el-dialog>
</template>
<script setup name="ComModal">
import FileUpload from '@/components/FileUpload'
import { getNoticeDetail, saveNotice, updateNotice } from '../api/index'
import { reactive, ref, toRefs } from 'vue'
import { ElMessage } from 'element-plus'

const visible = ref(false)
const loading = ref(false)
const formRef = ref(null)
const emits = defineEmits(['query'])

const data = reactive({
  form: {},
  title: '新增',
  handleType: null,
  rules: {
    title: [{
      required: true, message: '请输入标题', trigger: 'blur'
    }],
    content: [{
      required: true, message: '请输入内容', trigger: 'blur'
    }],
  },
})
const { form, title, handleType, rules } = toRefs(data)

const statusMap = new Map([
  ['add', '新增'],
  ['edit', '编辑'],
  ['view', '查看'],
])

const open = (type, id) => {
  title.value = statusMap.get(type)
  handleType.value = type
  visible.value = true
  if (id) getDetail(id)
}

const initForm = () => {
  form.value = getForm()
}
// 初始化
const getForm = () => {
  return {
    title: '',
    content: '',
    file: []
  }
}

onMounted(() => {
  initForm()
})

const getDetail = async (id) => {
  try {
    const res = await getNoticeDetail({
      id
    })
    form.value = res.data
  } catch (e) {
    throw Error(e)
  }
}

const success = (obj) => {
  form.value.file.push({
    image_name: obj.filename,
    image_link: obj['data-show-file'],
    usage_status: 1,
    image_size: `${obj.width} * ${obj.height}`
  })
}

const handleRemove = (index) => {
  form.value.file.splice(index, 1)
}

const save = () => {
  if (!formRef.value) return
  formRef.value.validate((valid) => {
    if (valid) {
      loading.value = true
      console.log(form.value)
      if (form.value.id) {
        updateNotice(form.value)
          .then((res) => {
            ElMessage.success(res.message)
            emits('query')
            close()
          })
          .finally((e) => {
            loading.value = false
          })
      } else {
        saveNotice(form.value)
          .then((res) => {
            ElMessage.success(res.message)
            emits('query')
            close()
          })
          .finally((e) => {
            loading.value = false
          })
      }
    }
  })
}
const close = () => {
  initForm()
  visible.value = false
}
defineExpose({ open })
</script>
<style lang="scss" scoped>
.borderNone {
  :deep(.el-input__wrapper) {
    border: none;
    box-shadow: none
  }
}
</style>
